/********************************************
*
*
*
*  test_mem.c
*
*
********************************************/


#ifdef __cplusplus
 #if __cplusplus
  extern "C"{
 #endif
#endif /* __cplusplus */


#include "test_suite.h"
#include "d_mem.h"
#include "d_task.h"
#include "d_show.h"
#include "d_queue.h"


VOID TEST_PT(UINT32 uil)
{
    UINT32 j = 100;
    UINT32 i;
    VOID * pAdd[100];

    PerformanceTestSaveTime();
    for(i=0; i<j; i++)
    {
        pAdd[i] = DMS_MemAlloc(uil);
        TEST_ASSERT_UEQUAL(NULL , pAdd[i]);
    }
    PerformanceTestShowTime("DMS_MemAlloc");

    PerformanceTestSaveTime();
    for(i=0; i<j; i++)
    {
        TEST_ASSERT_EQUAL(DMS_OK , DMS_MemFree(pAdd[i]));
    }
    PerformanceTestShowTime("DMS_MemFree");

    PerformanceTestSaveTime();
    for(i=0; i<j; i++)
    {
        pAdd[i] = malloc(uil);
        TEST_ASSERT_UEQUAL(NULL , pAdd[i]);
    }
    PerformanceTestShowTime("malloc");

    PerformanceTestSaveTime();
    for(i=0; i<j; i++)
    {
        free(pAdd[i]);
    }
    PerformanceTestShowTime("free");
}

UINT32 TEST_DMS_ALLOC_00()
{
    UINT32 i;

    while(1)
    {
	printf("\n Input size to test:");

	scanf("%d",&i);

	if(i==0)
	{
             return DMS_OK;
	}

    	TEST_PT(i);
    }

    return DMS_OK;
}

UINT32 Test_memCountNum = 0;

UINT32 TEST_DMS_ALLOC_01()
{
    UINT32 j;
    UINT32 uil;
    CHAR * pString[10240] = {NULL};

    for( uil = 0 ; uil < 10240 ; uil++ )
    {
        j = rand()%0xffff;
        pString[uil] = (CHAR * )DMS_MemAlloc(j);

        if(NULL != pString[uil])
        {
            DMS_MemSet((VOID *)pString[uil] , rand() & 0xff , j );
            Test_memCountNum ++;
        }
    }

    for( uil = 0 ; uil < 10240 ; uil++ )
    {
        if(NULL != pString[uil])
        {
            TEST_ASSERT_EQUAL(DMS_OK , DMS_MemFree( (VOID *)pString[uil]) );
        }
    }

    return DMS_OK;

}

extern UINT32 ulMaxAllocKval;
extern UINT32 ulMinAllocKval;
extern BuddyMemHead FreeListHead[];

UINT32 TEST_DMS_ALLOC_02()
{
    UINT32 i = 0xFF;
    UINT32 ulCount[32];

	for( i = ulMinAllocKval; i < ulMaxAllocKval ; i++ )
	{
		ulCount[i] = FreeListHead[i].ulCount ;
	}

    while(i--)
    {
        TEST_DMS_ALLOC_01();
    }

	for( i = ulMinAllocKval; i < ulMaxAllocKval ; i++ )
	{
	    TEST_ASSERT_EQUAL(ulCount[i] , FreeListHead[i].ulCount );
	}

    printf("Test_memCountNum %d\n",Test_memCountNum);

    return DMS_OK;

}

UINT32 TEST_DMS_ALLOC_03()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FREE_00()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FREE_01()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FREE_02()
{

    return DMS_OK;
}

UINT32 TEST_DMS_FREE_03()
{

    return DMS_OK;
}

VOID TEST_DMS_ALLOC()
{
    TEST_ASSERT_EQUAL(TEST_DMS_ALLOC_00(),DMS_OK);
    TEST_ASSERT_EQUAL(TEST_DMS_ALLOC_01(),DMS_OK);
    TEST_ASSERT_EQUAL(TEST_DMS_ALLOC_02(),DMS_OK);
    TEST_ASSERT_EQUAL(TEST_DMS_ALLOC_03(),DMS_OK);
}


VOID TEST_DMS_FREE()
{
    TEST_ASSERT_EQUAL(TEST_DMS_FREE_00(),DMS_OK);
    TEST_ASSERT_EQUAL(TEST_DMS_FREE_01(),DMS_OK);
    TEST_ASSERT_EQUAL(TEST_DMS_FREE_02(),DMS_OK);
    TEST_ASSERT_EQUAL(TEST_DMS_FREE_03(),DMS_OK);
}

VOID TEST_suite_mem(VOID)
{
    CU_pSuite suite;
    suite = CU_add_suite(__FUNCTION__, NULL, NULL);
    CU_ADD_TEST(suite, TEST_DMS_ALLOC);
    CU_ADD_TEST(suite, TEST_DMS_FREE);
}

#ifdef __cplusplus
 #if __cplusplus
  }
 #endif
#endif /* __cplusplus */
